package com.cqut.forum.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class MapKey {
	public static List<Map<String, Object>> changeMapKey(List<String> oldKey,
			List<String> newKey,List<Map<String, Object>> maps){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:maps){
			result.add(changeMapKey(oldKey,newKey,m));
		}
		return result;
	}
	public static Map<String, Object> changeMapKey(List<String> oldKey,List<String> newKey,Map<String, Object> map){
		Map<String, Object> result = new HashMap<String, Object>();
		for(int i =0;i<oldKey.size();i++){
			result.put(newKey.get(i), map.get(oldKey.get(i)));
		}
		return result;
	}
	public static List<Map<String, Object>> changeMapKey(List<Map<String, Object>> maps){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> m:maps){
			result.add(changeMapKey(m));
		}
		return result;
	}
	@SuppressWarnings("deprecation")
	public static Map<String, Object> changeMapKey(Map<String, Object> map){
		if(map==null){
			return null;
		}
		Map<String, Object> result = new HashMap<String, Object>();
		Object[] keys = map.keySet().toArray();
		for(Object kk:keys){
			String key = kk.toString();
			if(map.containsKey(key)){
				String[] k = key.split("_");
				int length = k.length;
				String newKey = length>0?k[0]:key;
				for(int i =1;i<length;i++){
					newKey = newKey + Character.toUpperCase(k[i].charAt(0))+k[i].substring(1, k[i].length());
				}
				if(map.get(key) instanceof Date){
					result.put(newKey, ((Date)map.get(key)).toLocaleString());
				}
				else{
					result.put(newKey, map.get(key));
				}
			}
		}
		
		return result;
	}
}
